package com.software.backend.searchmovie.service;

import com.software.backend.searchmovie.entity.Movie;
import com.software.backend.searchmovie.entity.Video;
import com.software.backend.searchmovie.service.repository.VideoRepository;
import com.software.backend.searchmovie.utils.FFmpegUtil;
import com.software.backend.searchmovie.utils.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: back-end
 * @description:
 * @author: C@t
 * @create: 2022-12-21 20:04
 **/
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoRepository videoRepository;
    @Autowired
    private HttpSession session;
    static FFmpegUtil fFmpegUtil = new FFmpegUtil();
    static QiniuCloudUtil qiniuCloudUtil = new QiniuCloudUtil();
    @Override
    public List<Video> findVideoById(int id) {
        Movie movie = (Movie) session.getAttribute(id + "movie");
        System.out.println(movie.toString());
        List<Video> videoList = (List<Video>) session.getAttribute(id + "videoList");
        for (Video video : videoList){
            try {
                String moviePath = movie.getPath();
                String startTime =video.getStartTime();
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                Date start = null;
                start = sdf.parse(startTime);
                long startTimeMills = start.getTime() + 28800000L;
                int videoDuration = video.getDuration();
                String videopath = null;
                System.out.println("剪辑开始时间："+new Date());
                videopath = fFmpegUtil.videoSpit(moviePath, startTimeMills, videoDuration);
                System.out.println("剪辑结束时间"+new Date());
                System.out.println("上传开始时间"+new Date());
                String upload = qiniuCloudUtil.upload(videopath, UUID.randomUUID().toString());
                System.out.println("上传结束时间"+new Date());
                video.setPath(upload);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        videoRepository.saveAll(videoList);
        //动态查询
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(criteriaBuilder.equal(root.get("movieId"), id));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        List<Video> all = videoRepository.findAll(specification);
//        System.out.println(all.size());
        return all;
    }

    @Override
    public String cleanVideo() {
        videoRepository.deleteAll();
        return "清理完毕";
    }
}

